#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define fi first
#define se second
#define endl "\n"
#define YES return cout << "YES" << endl, void()
#define NO return cout << "NO" << endl, void()
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const double PI = acos(-1);
const double eqa = (1+sqrt(5.0))/2.0;
const int mod = 1000000007, inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int dx[] = {0, 1, 0, -1, -1, 1, 1, -1};
int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
int gcd(int x, int y) {return y ? gcd(y, x % y) : x;}
int qmi(int a, int k, int p){int res = 1;while (k){if (k & 1) res = (LL)res * a % p;a = (LL)a * a % p;k >>= 1;}return res;}
int read(){int x = 0, f = 1;char ch = getchar();while (ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}return x * f;}
const int N = 200010;
void solve()
{
int n;
cin >> n;
vector<int> a(n + 1, 0);
for ( int i = 1; i <= n; i ++ ) cin >> a[i];
string s;
cin >> s;
s = ' ' + s;
vector<int> psa(n + 2, 0), ssa(n + 2, 0), psb(n + 2, 0), ssb(n + 2, 0);
for ( int i = 1; i <= n; i ++ ) {
if (s[i] == 'A') {
psa[i] = psa[i - 1] + a[i];
psb[i] = psb[i - 1];
}
else {
psb[i] = psb[i - 1] + a[i];
psa[i] = psa[i - 1];
}
// cout << psa[i] << " " << psb[i] << endl;
}
for ( int i = n; i >= 1; i -- ) {
if (s[i] == 'A') {
ssa[i] = ssa[i + 1] + a[i];
ssb[i] = ssb[i + 1];
} else {
ssb[i] = ssb[i + 1] + a[i];
ssa[i] = ssa[i + 1];
}
}
int mx = 0;
for ( int i = 1; i <= n; i ++ ) {
mx = max(mx, psa[i] - psb[i]);
}
for ( int i = n; i >= 1; i -- ) {
mx = max(mx, ssa[i] - ssb[i]);
}
cout << psb[n] + mx << endl;
}
signed main()
{
ios;
int t = 1;
// cin >> t;
while (t --)
{
solve();
}
return 0;
}
628. Maximum Product of Three Numbers | 1526A - Mean Inequality |
1526B - I Hate 1111 | 1881. Maximum Value after Insertion |
237. Delete Node in a Linked List | 27. Remove Element |
39. Combination Sum | 378. Kth Smallest Element in a Sorted Matrix |
162. Find Peak Element | 1529A - Eshag Loves Big Arrays |
19. Remove Nth Node From End of List | 925. Long Pressed Name |
1051. Height Checker | 695. Max Area of Island |
402. Remove K Digits | 97. Interleaving String |
543. Diameter of Binary Tree | 124. Binary Tree Maximum Path Sum |
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts | 501A - Contest |
160A- Twins | 752. Open the Lock |
1535A - Fair Playoff | 1538F - Interesting Function |
1920. Build Array from Permutation | 494. Target Sum |
797. All Paths From Source to Target | 1547B - Alphabetical Strings |
1550A - Find The Array | 118B - Present from Lena |